import os
count = 0
def subtract(timestamp, shift):
    #print timestamp
    hours = int(timestamp[:2])
    #print hours
    minutes = int(timestamp[3:5])
    seconds = int(timestamp[6:8])
    milseconds = int(timestamp[9:12])
    total = hours
    total = 60 * total + minutes
    total = 60 * total + seconds
    total = 1000 * total + milseconds
    total -= shift
    milseconds = total % 1000
    total /= 1000
    seconds = total % 60
    total /= 60
    minutes = total % 60
    total = total / 60
    hours = total
    def s(x, l=2):
        le = len(str(x))
        if le >= l:
            return str(x)
        else:
            return "0"*(l - le) + str(x)
    return s(hours) + ":" + s(minutes) + ":" + s(seconds) + "," + s(milseconds, 3)
def updateSubscene(path, start, shift):
    global count
    count = 0
    #updates the numbering of the sequences of the sentences
    f = open(path)
    lines = [x[:len(x) - 1] for x in f.readlines()]
    count = 0
    ret = ""
    for i in range(len(lines)):
        line = lines[i]
        # ------- fix numbers sequencing
        if line.isdigit() and lines[i + 1][2] == ':':
            count += 1
            ret += str(count) + "\n"
        elif "-->" in line and count >= start:
            print line
            begin = subtract(line[:12], shift)
            end = subtract(line[17:29], shift)
            print begin, end
            ret += begin + " --> " + end + "\n"
        else:
            ret += line + "\n"
  #  print ret
    f.close()
    f = open(path, 'w')
    f.write(ret)
    f.close()
  #  print ret
            
def calc(timestamp):
    hours = int(timestamp[:2])
    #print hours
    minutes = int(timestamp[3:5])
    seconds = int(timestamp[6:8])
    milseconds = int(timestamp[9:12])
    total = hours
    total = 60 * total + minutes
    total = 60 * total + seconds
    total = 1000 * total + milseconds
    return total
path = "test.txt"
shift = 1025500
#first: shift= 80670, at 141, remove 141-151
#second : shift=169030, at 157 remove 157-175
#3rd : shift=,at 273 remove 273-293,
#4th" shift=177760, at 412, remove 412-429
#5th shift=, at 572, remove 572-639
s1 = "01:13:56,920"
s2 = "01:07:25,000"
shift = calc(s1) - calc(s2)
print shift, subtract(s1, shift)
updateSubscene(path, 572, -100)
